package server.multiServlet;

import com.google.common.collect.Lists;
import lombok.*;
import server.Request;
import server.Response;

import java.util.List;

@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor(staticName = "of")
public class Host implements Container {

    @NonNull
    private String name;

    @NonNull
    private String appBase;

    private List<Context> contexts = Lists.newArrayList();

    @Override
    public void init() {
        contexts.forEach(mapperContext -> {
            mapperContext.setName(appBase);
            mapperContext.init();
        });
    }

    @Override
    public void execute(Request request, Response response) {
        for (Context mapperContext : contexts) {
            if (request.getUrl().startsWith(mapperContext.getPath())) {
                mapperContext.execute(request, response);
            }
        }
    }
}
